package com.byt.common.view.finder.addresswheelfinder.picker;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;

import com.byt.common.view.finder.AddressHelper;
import com.byt.common.view.finder.entity.Province;

import java.util.ArrayList;

/**
 * 获取地址数据并显示地址选择器
 */
public class AddressTask extends AsyncTask<String, Void, ArrayList<Province>> {
    private Activity activity;
    private ProgressDialog dialog;
    private Callback callback;

    public AddressTask(Activity activity) {
        this.activity = activity;
    }


    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(activity, null, "正在初始化数据...", true, true);
    }

    @Override
    protected ArrayList<Province> doInBackground(String... params) {
        return AddressHelper.getAddressProvice(activity);
    }

    @Override
    protected void onPostExecute(ArrayList<Province> result) {
        dialog.dismiss();
        if (callback != null) {
            if (result.size() > 0) {
                callback.onAddressInitSuccess(result);
            } else {
                callback.onAddressInitFailed();
            }
        }
    }

    public interface Callback {

        void onAddressInitFailed();

        void onAddressInitSuccess(ArrayList<Province> result);
    }
}
